define(function (require, exports, module) {
    var template = require("template");
    var Ajax = require("ajax");
    var layer = require("layer");
    var tool = require('tool');
    var report = function (callback) {
        this.init.apply(this,arguments);
    };
    report.prototype = {
        constructor: report,
        options: {},
        init: function (options) {
            var t = this;
            t.options = $.extend({},t.options,options);
            $(".tipConNav").text("日报");
            $(window).off('scroll');
            //加载日报列表
            t.loadDailyList(1,15);
			//滚动条置顶
			window.scrollTo(0,0);
        },
        loadDailyList:function(pageIndex,pageSize){
        	var t=this;
       	 	Ajax.ajax({
                url:localhost + "report/selectReport.do",
				data: {
					pageIndex: pageIndex,
					pageSize: pageSize
				},
                beforeSend:function(){
					if(pageIndex==1){
						$('.tbody').html(template("loadingDom"));
					}else{
						$("#listDom").html('');
						$("#listDom").append("<div class='loadingMoreBox animated fadeIn nextloding'><span>正在加载中，请稍后</span></div>");
					}
                },
                success:function(data){
                	var href = window.location.href.indexOf("admin/report");
    				if(href < 0 ) {
    					return;
    				}
                	var item=data.returnData;
//                	$('.tbody').html('');
                	if(item.length>0){
                		$(".loadingBox").remove();
						$(".loadingMoreBox").remove();
                		for(var i=0;i<item.length;i++){
                			item[i].reportTime=(item[i].reportTime).substring(0,10);
                			var id=tool.LockerStr.encrypt((item[i].reportId).toString(),'yq');
                			item[i].url=localhost+"views/admin.jsp#!admin/reportDetail/reportId:"+id;
                    		$('.tbody').append(template('liTemp',item[i]));
                    	}
		                if(href > -1 ) {
		                	t.loadPage(pageIndex);
		                }
		                if(!(pageIndex==1 && item.length<15)){
		                	$("#listDom").append("<div class='morePage animated fadeIn'><span>下拉加载更多</span></div>");
		                }
		                
                	}else{
                		$(".loadingBox").remove();
						$(".loadingMoreBox").remove();
                		$(window).off('scroll');
                		if(pageIndex==1){
                			$('.tbody').html(template('noDataDom'));
                		}else{
                			 $("#listDom").append("<div class='lastPage animated fadeIn'><span>没有更多内容</span></div>");
                		}
                	}
                }
            });
        },
        
        loadPage: function(pageIndex) {
			var t = this;
			//获取页面顶部被卷起来的高度
			function scrollTop() {
				return Math.max(
					//chrome
					document.body.scrollTop,
					//firefox/IE
					document.documentElement.scrollTop);
			}
			//获取页面文档的总高度
			function documentHeight() {
				//现代浏览器（IE9+和其他浏览器）和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
				return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
			}
			function windowHeight() {
				return(document.compatMode == "CSS1Compat") ?
					document.documentElement.clientHeight :
					document.body.clientHeight;
			}
			//debugger;
			if(window.location.href.indexOf("admin/report")>-1 && (window.location.href.indexOf("Detail")==-1)){
			}else{
				return false;
			}
			var isloading = false;
			$(window).off('scroll');
			$(window).on('scroll', function() {
				//判定是否加载中，防止重复加载
				if(!isloading){
					//滚动条卷起来的高度 + 窗口高度 >= 文档的总高度 - 50/*这里将滚动响应区域高度取50px*/；如果这个判断为true则表示滚动条滚动到了底部。
					if(scrollTop() + windowHeight() >= (documentHeight() - 50)) {
						isloading = false;
						pageIndex = parseInt(pageIndex)+ 1;
						if(window.location.href.indexOf("admin/report")>-1 && (window.location.href.indexOf("Detail")==-1)){
						}else{
							return false;
						}
						t.loadDailyList(pageIndex,15);
						isloading = true;
					}
				}
			});
		}
    };
    module.exports = report;
});